/*
OpenMVL Matrix Vector Library
Copyright (c) 2009 FluidInteractive (R)
 
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising
from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
 
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
 
Written by: Nicola Candussi <nicola@fluidinteractive.com>
*/

//expr_skew_symmetric.h

#ifndef MVL_EXPR_SKEW_SYMMETRIC_HPP
#define MVL_EXPR_SKEW_SYMMETRIC_HPP

#include "expr.hpp"
#include "layout_skew_symmetric.hpp"

namespace mvl {

template<typename E>
class expr_skew_symmetric : public expr< expr_skew_symmetric<E> > 
{ 
public:
    typedef expr_skew_symmetric self_t;

    //make sure that the input is in column major order
    typedef const expr_order<layout::column_major_tag, const E>     expr_t;
    typedef typename expr_t::const_closure_t                        expr_closure_t; 

public:
    typedef typename layout::layout_skew_symmetric<typename expr_t::layout_t>::type layout_t;
    typedef typename expr_t::value_type     value_type;
    typedef typename layout_t::order        order;
    typedef typename layout_t::Rows         Rows;
    typedef typename layout_t::Cols         Cols;
    typedef typename layout_t::Size         Size;

    typedef const self_t const_closure_t;
    typedef const_closure_t closure_t;

public:
    expr_skew_symmetric(expr_t const& e) : m_e(e) { };

    //access
    template<typename Entry>
    value_type at() const {
        return at<typename Entry::src>(typename Entry::neg());
    }

private:
    template<typename Entry>
    value_type at(meta::bool_<false>) const {
        return m_e.template at<Entry>();
    }

    template<typename Entry>
    value_type at(meta::bool_<true>) const {
        return -m_e.template at<Entry>();
    }

private:
    expr_closure_t m_e;
};

template<typename E>
struct skew_symmetrix_traits 
{
    typedef const expr_skew_symmetric<E> type;
};

//generates a skew-symmetric matrix for cross product. The input must be a column vector
template<typename E>
inline 
typename skew_symmetrix_traits<E>::type
skew_symmetric(expr<E> const& e) {
    MVL_STATIC_CHECK((E::Cols::value == 1 && E::Rows::value >= 3), "vector size mismatch");
    typedef typename skew_symmetrix_traits<E>::type expr_t;
    return expr_t(e());
}

} // namespace mvl

#endif

